import requests
from lxml import etree
import lxml.html
from mysqlDb.db import ConDb

school_url = "https://www.dhu.edu.cn"
school = "东华大学"
con = ConDb()
li = [
    # {'name': '纺织学院', 'url': 'https://www.dhu.edu.cn/5955/list.htm'},
    # {'name': '服装与艺术设计学院', 'url': 'https://www.dhu.edu.cn/5956/list.htm'},
    # {'name': '材料科学与工程学院', 'url': 'https://www.dhu.edu.cn/5957/list.htm'},
    # {'name': '化学化工与生物工程学院', 'url': 'https://www.dhu.edu.cn/5958/list.htm'},
    # {'name': '旭日工商管理学院', 'url': 'https://www.dhu.edu.cn/5959/list.htm'},
    # {'name': '信息科学与技术学院', 'url': 'https://www.dhu.edu.cn/5960/list.htm'},
    # {'name': '计算机科学与技术学院', 'url': 'https://www.dhu.edu.cn/5961/list.htm'},
    # {'name': '环境科学与工程学院', 'url': 'https://www.dhu.edu.cn/5962/list.htm'},
    # {'name': '机械工程学院', 'url': 'https://www.dhu.edu.cn/5963/list.htm'},
    # {'name': '理学院', 'url': 'https://www.dhu.edu.cn/5964/list.htm'},
    # {'name': '人文学院', 'url': 'https://www.dhu.edu.cn/5965/list.htm'},
    # {'name': '马克思主义学院', 'url': 'https://www.dhu.edu.cn/mkszyxy_10295/list.htm'},
    # {'name': '外语学院', 'url': 'https://www.dhu.edu.cn/5966/list.htm'},
    # {'name': '功能材料研究中心', 'url': 'https://www.dhu.edu.cn/5967/list.htm'},
    # {'name': '分析测试中心', 'url': 'https://www.dhu.edu.cn/fxcszx_7773/list.htm'},
    # {'name': '国际文化交流学院', 'url': 'https://www.dhu.edu.cn/5968/list.htm'},
    # {'name': '体育部', 'url': 'https://www.dhu.edu.cn/5969/list.htm'},
    # {'name': '图书馆', 'url': 'https://www.dhu.edu.cn/5970/list.htm'},
    # {'name': '先进低维材料中心', 'url': 'https://www.dhu.edu.cn/xjdwclzx_11566/list.htm'},
    # {'name': '纺织科技创新中心', 'url': 'https://www.dhu.edu.cn/5971/list.htm'},
    # {'name': '上海国际时尚科创中心', 'url': 'https://www.dhu.edu.cn/shgjsskczx_15247/list.htm'},
    {'name': '出版社', 'url': 'https://www.dhu.edu.cn/cbs/list.htm'},
    {'name': '人工智能研究院', 'url': 'https://www.dhu.edu.cn/rgznyjy/list.htm'}]

r = requests.session()
for dict_object in li:
    academy = dict_object["name"]
    academy_url = dict_object["url"]
    t = r.get(academy_url).text
    html = t.encode("ISO-8859-1").decode("utf-8")
    etr = etree.HTML(html)
    table = etr.xpath('//table[@class="wp_article_list_table"]')
    table_dom = lxml.html.tostring(table[0])
    tr_etr = etree.HTML(table_dom)
    tr_list = tr_etr.xpath('//tr')

    for tr in tr_list:
        tr_dom = lxml.html.tostring(tr)
        tr_etr1 = etree.HTML(tr_dom)
        in_tr = tr_etr1.xpath('//tr/td/table/tbody/tr')
        try:
            tr_dom_in = lxml.html.tostring(in_tr[0]).replace(b'\r\n', b'').replace(b'  ', b'')
        except IndexError as e:
            continue
            # tr_dom_in = lxml.html.tostring(tr_etr1).replace(b'\r\n', b'').replace(b'  ', b'')
        t = etree.HTML(tr_dom_in)
        name = t.xpath("//tr/td[1]/a/text()")
        if len(name) > 0:
            name = name[0]
        else:
            name = ''
        url1 = t.xpath("//tr/td[1]/a/@href")
        list_url = academy_url
        if len(url1) > 0:
            mainurl = school_url + url1[0]
        else:
            mainurl = ''

        professional = t.xpath("//tr/td[3]/text()")
        if len(professional) > 0:
            professional = professional[0]
        else:
            professional = ''

        supervise_stutent = t.xpath("//tr/td[4]/text()")
        if len(supervise_stutent) > 0:
            supervise_stutent = supervise_stutent[0]
        else:
            supervise_stutent = ''

        # print(name, mainurl, school_url, school, academy, academy_url, list_url, professional, supervise_stutent)
        li111 = [name, mainurl, school_url, school, academy, academy_url, list_url, professional, supervise_stutent]
        con.insert_TB("talent_info", li111,
                      "name", "mainurl", "school_url", "school", "academy", "academy_url", "list_url", "professional",
                      "supervise_stutent")
